package com.lutzyt.delivery.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lutzyt.delivery.bean.PermissionMenu;
import com.lutzyt.delivery.mapper.PermissionMenuDAO;
import com.lutzyt.delivery.service.PermissionMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author zyt
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PermissionMenuServiceImpl extends ServiceImpl<PermissionMenuDAO, PermissionMenu> implements PermissionMenuService {

  public static final String PERMISSION_ID = "permission_id";
  public static final String MENU_ID = "menu_id";

  private PermissionMenuDAO permissionMenuDAO;


  @Transactional(readOnly = true)
  @Override
  public PermissionMenu findById(Integer id) {
    List<PermissionMenu> list = this.query().eq(PERMISSION_ID, id).list();
    // 把权限的所有菜单id拿出来放到ids里面
    List<Integer> menuIds = list.stream().map(PermissionMenu::getMenuId).collect(Collectors.toList());
    PermissionMenu permissionMenu = new PermissionMenu();
    permissionMenu.setPermissionId(id);
    permissionMenu.setMenuIds(menuIds);
    return permissionMenu;
  }

  @Override
  public List<Integer> findMenuIdByIds(List<Integer> permissionIds) {
    List<PermissionMenu> list = this.query().in(PERMISSION_ID, permissionIds).list();
    // 把权限的所有菜单id拿出来放到ids里面
    List<Integer> menuIds = list.stream().map(PermissionMenu::getMenuId).collect(Collectors.toList());
    return menuIds;
  }

  @Override
  public IPage<PermissionMenu> findByPage(int pageNum, int pageSize) {
    return permissionMenuDAO.selectPage(new Page<>(pageNum, pageSize), null);
  }

  @Override
  public void insert(PermissionMenu permissionMenu) {
    // 当前需要添加的menu
    List<Integer> menuIds = permissionMenu.getMenuIds();
    // 删除不在 menuIds集合中的记录
    this.update()
            .eq(PERMISSION_ID, permissionMenu.getPermissionId())
            .notIn(!menuIds.isEmpty(), MENU_ID, menuIds)
            .remove();
    // 获取数据库中的menuIds
    List<Integer> menuIdList = this.findById(permissionMenu.getPermissionId()).getMenuIds();
    List<PermissionMenu> list = new ArrayList<>();
    // 去掉存在已经存在的menuId，把新添加的放入list
    menuIds.stream().filter(bean->!menuIdList.contains(bean)).forEach(menuId -> {
      PermissionMenu bean = new PermissionMenu();
      bean.setMenuId(menuId);
      bean.setPermissionId(permissionMenu.getPermissionId());
      list.add(bean);
    });
    // 如果list有数据则批量插入
    if (list.size() > 0){
      this.saveBatch(list);
    }
  }

  @Override
  public void update(PermissionMenu permissionMenu) {
    permissionMenuDAO.updateById(permissionMenu);
  }

  @Override
  public void deleteById(Integer id) {
    permissionMenuDAO.deleteById(id);
  }



  @Autowired
  public void setPermissionMenuDAO(PermissionMenuDAO permissionMenuDAO) {
    this.permissionMenuDAO = permissionMenuDAO;
  }
}